import { Controller, IUpdateResult } from 'egg';

export default class CategoriesController extends Controller {
  mysql = this.app.mysql as any;
  public async index() {
    const { ctx } = this;
    const categories = await ctx.service.categories.get();
    ctx.body = {
      code: 0,
      message: '成功',
      result: {
        categories
      },
    };
  }
  public async statistics() {
    const { ctx } = this;
    const category_statistics = await ctx.service.categories.get(undefined,undefined,true);
    ctx.body = {
      code: 0,
      message: '成功',
      result: {
        category_statistics
      },
    };
  }
  public async deleteById(){
    const { ctx } = this;
    const {id} = ctx.params;
    const admin = this.ctx.user_id === undefined ? false : true;
    if (!admin) {
      ctx.status = 401;
      ctx.body = {
        code: 1,
        message: "未登录",
      };
      return;
    }
    // 删除该类别文章
    await this.mysql.delete('articles',{
      category_id: id
    })
    const resultCategory:IUpdateResult = await this.mysql.delete('categories', {
      id
    })
    if (resultCategory.affectedRows > 0){
      ctx.body = {
        code: 0,
        message: "删除成功",
      };
    } else {
      ctx.status = 404;
      ctx.body = {
        code: 1,
        message: "未找到对应类别",
      };
    }
  }

  public async add(){
    const { ctx } = this;
    // const {id} = ctx.params;
    const admin = this.ctx.user_id === undefined ? false : true;
    if (!admin) {
      ctx.status = 401;
      ctx.body = {
        code: 1,
        message: "未登录",
      };
      return;
    }
    const {alias} = ctx.request.body;
    this.ctx.validate({
      "alias": {
        type: 'string',
      }
    })
    const count = await this.mysql.count('categories', {
      alias
    });
    if (count>0){
      ctx.body = {
        code: 1,
        message: "类别已存在",
      };
      return
    };
    const resultCategory:IUpdateResult = await this.mysql.insert('categories', {
      alias
    });
    if (resultCategory.affectedRows > 0){
      ctx.body = {
        code: 0,
        message: "新建类别成功",
        result: {
          category_id: resultCategory.insertId
        }
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: 1,
        message: "插入失败",
        result : resultCategory
      };
    }
  }

  public async rename(){
    const { ctx } = this;
    const {id} = ctx.params;
    const admin = this.ctx.user_id === undefined ? false : true;
    if (!admin) {
      ctx.status = 401;
      ctx.body = {
        code: 1,
        message: "未登录",
      };
      return;
    }
    const {alias} = ctx.request.body;
    this.ctx.validate({
      "alias": {
        type: 'string',
      }
    })
    const resultCategory:IUpdateResult = await this.mysql.update('categories', {
      id,
      alias
    })
    if (resultCategory.affectedRows > 0){
      ctx.body = {
        code: 0,
        message: "修改成功",
      };
    } else {
      ctx.status = 404;
      ctx.body = {
        code: 1,
        message: "未找到对应类别",
      };
    }
  }
}
